package cn.schoolwow.quickdao.module.initial.flow.database;

import cn.schoolwow.quickdao.domain.database.parent.DatabaseType;
import cn.schoolwow.quickflow.domain.FlowContext;
import cn.schoolwow.quickflow.flow.BusinessFlow;

public class SetQueryDatabaseStatementFlow implements BusinessFlow {
    @Override
    public void executeBusinessFlow(FlowContext flowContext) throws Exception {
        String databaseName = flowContext.checkData("databaseName", String.class);
        DatabaseType databaseType = flowContext.checkInstanceData(DatabaseType.class);

        String queryDatabaseSQL = null;
        switch (databaseType){
            case H2:{queryDatabaseSQL = "select schema_name from information_schema.schemata where schema_name = '"+databaseName+"';";}break;
            case MariaDB:
            case Mysql:{queryDatabaseSQL = "select schema_name from information_schema.schemata where schema_name = '"+databaseName+"';";}break;
            case Postgresql:{queryDatabaseSQL = "select datname from pg_database where datname = '"+databaseName+"';";}break;
            case SQLServer:{queryDatabaseSQL = "select name from sys.databases where name = '"+databaseName+"'”;";}break;
            case Oracle:{queryDatabaseSQL = "select table_name from user_tables where table_name = '"+databaseName+"';";}break;
            default:{
                flowContext.brokenCurrentCompositeBusiness("不支持的数据库类型!"+databaseType.name());
            }break;
        }
        flowContext.putTemporaryData("queryDatabaseSQL", queryDatabaseSQL);
    }

    @Override
    public String name() {
        return "设置查询数据库是否存在语句";
    }
}
